package com.pro.staffmanage.controller;

import com.pro.staffmanage.mapper.DepartmentMapper;
import com.pro.staffmanage.mapper.EmployeeMapper;
import com.pro.staffmanage.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: Haotian Cui
 * @Date: 2022/7/22 - 07 - 22 - 16:17
 * @Description: com.pro.staffmanage.controller
 * @Project: staffmanage
 * @version: 1.0
 */
@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @GetMapping("/all")
    public String getEmployeeList(HttpSession session){
        session.setAttribute("list", employeeMapper.queryAllEmployee());
        session.setAttribute("dlist", departmentMapper.queryAllDepartment());
        //  (Guess)The list here is MyWebMvcConfiguration,
        //  only Redirect can be used in ViewController of WebMvcConfigurer
        return "redirect:/emplist";
    }

    @PostMapping("/add")
    /**
     * @RequestParam("id") Integer id,
     *             @RequestParam("lastname") String lastname,
     *             @RequestParam("email") String email,
     *             @RequestParam("gender") Integer gender,
     *             @RequestParam("department") Integer department,
     *             @RequestParam("birth") String birth
     */
    public String addEmployee(Employee employee){
        employeeMapper.addEmployee(employee);
        return "emp/list";
    }

    @GetMapping("/query/{id}")
    public String queryEmployeeById(@PathVariable("id")Integer id, Model model){
        model.addAttribute("upEmp", employeeMapper.queryEmployeeById(id));
        return "emp/list";
    }

    @PutMapping("/update")
    public String updateEmployee(Employee employee){
        employeeMapper.updateEmployee(employee);
        return "emp/list";
    }

    @DeleteMapping("/delete/{id}")
    public String DeleteEmployee(@PathVariable("id")Integer id){
        employeeMapper.deleteEmployee(id);
        return "emp/list";
    }

}
